import type { ErrorMessageMode } from '/#/axios';
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { ValidateCodeInfo } from '/@/api/shop-gateway/model/captchaModel';
import { captchaApi } from '/@/api/shop-gateway/captcha';

interface CaptchaState {
  img?: string;
  uuid?: string;
}

export const useCaptchaStore = defineStore({
  id: 'app-gateway-captcha',
  state: (): CaptchaState => ({
    img: undefined,
    uuid: undefined,
  }),
  actions: {
    /**
     * @description: validateCode
     */
    async getValidateCode(mode?: ErrorMessageMode): Promise<ValidateCodeInfo | null> {
      try {
        return await captchaApi(mode);
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },
  },
});

// Need to be used outside the setup
export function useCaptchaStoreWithOut() {
  return useCaptchaStore(store);
}
